package org.example._03_consumer_way;

import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
import org.apache.rocketmq.client.consumer.PullResult;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.创建一个拉取消息的消费者对象
        DefaultMQPullConsumer defaultMQPullConsumer = new DefaultMQPullConsumer("rocketmq-consumer");
        //2.设置名字地址(和 nameser 进行绑定)
        defaultMQPullConsumer.setNamesrvAddr("127.0.0.1:9876");
        //3 启动消费者
        defaultMQPullConsumer.start();
        //4 主动上MQ中拉取信息
        PullResult pullResult = defaultMQPullConsumer.pull(
                new MessageQueue("consumer-way",
                        "broker-a",//"broker-a"是默认的brokerName
                        3),
                "*",
                0,
                10);
        List<MessageExt> msgFoundList = pullResult.getMsgFoundList();
        for (MessageExt messageExt : msgFoundList) {
            System.out.println("消息的内容为:" + new String(messageExt.getBody()));
        }

    }
}